package src.greed;

public class no860 {//柠檬水找零

    public static void main(String[] args) {
        int[] bill = {5, 5, 10, 20, 5, 5, 5, 5, 5, 5, 5, 5, 5, 10, 5, 5, 20, 5, 20, 5};
        System.out.println(lemonadeChange(bill));
    }

    public static boolean lemonadeChange(int[] bills) {
        int num5 = 0, num10 = 0;
        for (int bill : bills) {
            if (bill == 5) num5++;
            if (bill == 10) {
                num10++;
                num5--;
                if (num5 < 0) return false;
            }
            if (bill == 20) {
                if (num10 > 0 && num5 > 0) {  //有10+5时找10+5
                    num10--;
                    num5--;
                } else if (num5 >= 3) {  //没10元时找3个5块
                    num5 -= 3;
                } else return false;

                //if (num5 < 0 || num10 < 0) return false;
            }
        }
        return true;

    }
}
